//package com.feng.study.config;
//
//import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.common.collect.Maps;
//import org.springframework.cache.CacheManager;
//import org.springframework.cache.annotation.CachingConfigurerSupport;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.cache.interceptor.KeyGenerator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.RedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
//import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
//import org.springframework.util.DigestUtils;
//
//import java.util.Map;
//
//@Configuration
//@EnableCaching
//public class RedisConfig extends CachingConfigurerSupport {
//
//    public static final String KEY_GENERATOR = "wiselyKeyGenerator";
//
//    public static final String EXPIRE_1D = "product:1d";
//    public static final String EXPIRE_12H = "product:12h";
//    public static final String EXPIRE_4H = "product:4h";
//    public static final String EXPIRE_1H = "product:1h";
//    public static final String EXPIRE_30M = "product:30m";
//    public static final String EXPIRE_10M = "product:10m";
//    public static final String EXPIRE_5M = "product:5m";
//    public static final String EXPIRE_1M = "product:1m";
//    public static final String EXPIRE_30S = "product:30s";
//    public static final String EXPIRE_10S = "product:10s";
//
//
//    @Bean
//    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
//        StringRedisTemplate template = new StringRedisTemplate(factory);
//        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setVisibility(PropertyAccessor.ALL, Visibility.ANY);
//        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
//        return template;
//    }
//
////    @Bean
////    public CacheManager redisCacheManager(RedisTemplate redisTemplate) {
////        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
////        //防止生成zSet存储key值
////        redisCacheManager.setUsePrefix(true);
////        redisCacheManager.setDefaultExpiration(60L);
////
////        Map<String, Long> expireByCacheName = Maps.newHashMap();
////        expireByCacheName.put(EXPIRE_1D, 60 * 60 * 24L);
////        expireByCacheName.put(EXPIRE_12H, 60 * 60 * 12L);
////        expireByCacheName.put(EXPIRE_4H, 60 * 60 * 4L);
////        expireByCacheName.put(EXPIRE_1H, 60 * 60L);
////        expireByCacheName.put(EXPIRE_30M, 60 * 30L);
////        expireByCacheName.put(EXPIRE_10M, 60 * 10L);
////        expireByCacheName.put(EXPIRE_5M, 60 * 5L);
////        expireByCacheName.put(EXPIRE_1M, 60L);
////        expireByCacheName.put(EXPIRE_30S, 30L);
////        expireByCacheName.put(EXPIRE_10S, 10L);
////
////        redisCacheManager.setExpires(expireByCacheName);
////        return redisCacheManager;
////    }
//
//    @Bean
//    public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
//        return redisTemplate.opsForValue();
//    }
//
//    @Bean
//    public KeyGenerator wiselyKeyGenerator() {
//        return (target, method, params) -> {
//            StringBuilder sbParams = new StringBuilder(target.getClass().getName());
//            if (params != null) {
//                for (Object param : params) {
//                    if (param != null) {
//                        sbParams.append(param.toString());
//                    }
//                }
//            }
//            return target.getClass().getSimpleName()
//                    + ":"
//                    + method.getName()
//                    + ":"
//                    + DigestUtils.md5DigestAsHex(sbParams.toString().getBytes());
//        };
//    }
//
//}